package com.xiaodu.acer.phoneplay.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.xiaodu.acer.phoneplay.App;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.AppInfo;
import com.xiaodu.acer.phoneplay.bean.Banner;
import com.xiaodu.acer.phoneplay.bean.IndexBean;
import com.xiaodu.acer.phoneplay.ui.activity.AppDetailActivity;
import com.xiaodu.acer.phoneplay.ui.activity.HotAppActivity;
import com.xiaodu.acer.phoneplay.ui.activity.HotGameActivity;
import com.xiaodu.acer.phoneplay.ui.activity.SubjectActivity;
import com.xiaodu.acer.phoneplay.ui.decoration.DividerItemDecoration;
import com.xiaodu.acer.phoneplay.widget.BannerLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import library.imageloader.ImageLoader;
import retrofit2.Retrofit;

/**
 * 推荐Adapter 多条目的Adapter
 */
public class RecomendAdatper extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements View.OnClickListener {

    private Context mContext;
    private IndexBean mIndexBean;

    public static final int TYPE_BANNER = 1;
    private static final int TYPE_ICON = 2;
    private static final int TYPE_APPS = 3;
    private static final int TYPE_GAMES = 4;

    private LayoutInflater mLayoutInflater;

   // private RxDownload mRxDownload;
    private Retrofit mRetrofit;

    public RecomendAdatper(Context context,Retrofit retrofit) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
     //   this.mRxDownload = rxDownload;
        this.mRetrofit=retrofit;
    }

    public void setDatas(IndexBean indexBean) {
        this.mIndexBean = indexBean;
    }

    public IndexBean getDatas() {
        return mIndexBean;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_BANNER;
            case 1:
                return TYPE_ICON;
            case 2:
                return TYPE_APPS;
            case 3:
                return TYPE_GAMES;
            default:
                return super.getItemViewType(position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_BANNER) {
            return new BannerViewHolder(mLayoutInflater.inflate(R.layout.template_banner, parent, false));
        } else if (viewType == TYPE_ICON) {
            return new NavIconViewHolder(mLayoutInflater.inflate(R.layout.template_nav_icon, parent, false));
        } else if (viewType == TYPE_APPS) {
            return new AppViewHolder(mLayoutInflater.inflate(R.layout.template_recyleview_with_title, null, false), TYPE_APPS);
        } else if (viewType == TYPE_GAMES) {
            return new AppViewHolder(mLayoutInflater.inflate(R.layout.template_recyleview_with_title, null, false), TYPE_GAMES);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;

            final List<Banner> banners = mIndexBean.getBanners();
            List<String> urls = new ArrayList<>(banners.size());

            for (Banner banner : banners) {
                urls.add(banner.getThumbnail());
            }
            bannerViewHolder.mBanner.setViewUrls(urls);
            bannerViewHolder.mBanner.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Banner banner = banners.get(position);
                    if (banner.getAction().equals("subject")) {
                    } else {
                    }
                }
            });
        } else if (position == 1) {
            NavIconViewHolder bannerViewHolder = (NavIconViewHolder) holder;

            bannerViewHolder.mLayoutHotApp.setOnClickListener(this);
            bannerViewHolder.mLayoutHotGame.setOnClickListener(this);
            bannerViewHolder.mLayoutHotSubject.setOnClickListener(this);
        } else {
            AppViewHolder viewHolder = (AppViewHolder) holder;

            final AppInfoAdapter appInfoAdapter = AppInfoAdapter.builder()
                    .showBrief(true)
                    .showCategoryName(false)
                    .showPosition(false)
                //    .rxDownload(mRxDownload)
                    .retrofit(mRetrofit)
                    .build();

            if (viewHolder.type == TYPE_APPS) {
                viewHolder.mText.setText("热门应用");
                appInfoAdapter.addData(mIndexBean.getRecommendApps());
            } else {
                viewHolder.mText.setText("热门游戏");
                appInfoAdapter.addData(mIndexBean.getRecommendGames());
            }

            viewHolder.mRecyclerView.setAdapter(appInfoAdapter);

            viewHolder.mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    AppInfo appInfo = appInfoAdapter.getItem(position);

                    //强转成application，解决new App()带来的不是单例的异常
                    App applicationContext = (App) mContext.getApplicationContext();
                    applicationContext.setView(view);
                    Intent intent = new Intent(mContext, AppDetailActivity.class);
                    intent.putExtra("appinfo", appInfo);
                    mContext.startActivity(intent);
                }
            });


        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_hot_app:   //热门应用
                mContext.startActivity(new Intent(mContext, HotAppActivity.class));
                break;
            case R.id.layout_hot_game:  //热门游戏
                mContext.startActivity(new Intent(mContext, HotGameActivity.class));
                break;
            case R.id.layout_hot_subject:   //热门主题
                mContext.startActivity(new Intent(mContext, SubjectActivity.class));
                break;
            default:
                break;

        }


    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_icon)
        ImageView mImgIcon;
        @BindView(R.id.text_title)
        TextView mTextTitle;
        @BindView(R.id.text_size)
        TextView mTextSize;
        @BindView(R.id.btn_dl)
        Button mBtnDl;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class BannerViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.banner)
        BannerLayout mBanner;

        public BannerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            mBanner.setImageLoader(new ImgLoader());
        }
    }

    class NavIconViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.layout_hot_app)
        LinearLayout mLayoutHotApp;
        @BindView(R.id.layout_hot_game)
        LinearLayout mLayoutHotGame;
        @BindView(R.id.layout_hot_subject)
        LinearLayout mLayoutHotSubject;

        NavIconViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    public class AppViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.text)
        TextView mText;
        @BindView(R.id.recycler_view)
        RecyclerView mRecyclerView;


        int type;

        public AppViewHolder(View itemView, int type) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.type = type;
            initRecyclerView();


        }

        private void initRecyclerView() {

            mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext) {

                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });

            DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST);

            mRecyclerView.addItemDecoration(itemDecoration);

        }
    }

    class ImgLoader implements BannerLayout.ImageLoader {

        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            ImageLoader.load(path, imageView);
        }
    }


}
